//
//  STSetingViewController.m
//  STBasicsApp
//
//  Created by jerry on 2019/5/6.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STSetingViewController.h"
#import "STStingTableViewCell.h"
#import "STSetingGroupModel.h"

#import "STThemeSetViewController.h"
#import "STFeedBackViewController.h"
#import "STAboutUsViewController.h"
#import "STMessageSetViewController.h"

@interface STSetingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) NSMutableArray<STSetingGroupModel *> * dataArrs;

@end

@implementation STSetingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNav];
    [self showView];
    [self initSetData];
}

- (void)configNav{
    self.title = ASLocalizedString(@"设置");
}

- (void)showView{
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    
}

- (void)initSetData{
    
    //主题管理
    STSetingItemModel * themeItemModel = [[STSetingItemModel alloc]init];
    themeItemModel.leftIcon  = @"st_icon_nav_setting";
    themeItemModel.leftTitle = @"主题管理";
    themeItemModel.itemType  = STSetingItemTypeNormal;
    themeItemModel.jumpType  = STSetingItemJumpTypeNormal;
//    themeItemModel.rightTitle01 = @"测试01";
//    themeItemModel.rightTitle02 = @"测试02";
    themeItemModel.controller= NSStringFromClass([STThemeSetViewController class]);
    
    //消息管理
    STSetingItemModel * msgItemModel = [[STSetingItemModel alloc]init];
    msgItemModel.leftIcon  = @"st_icon_nav_setting";
    msgItemModel.leftTitle = @"消息管理";
    msgItemModel.itemType  = STSetingItemTypeNormal;
    msgItemModel.jumpType  = STSetingItemJumpTypeNormal;
    msgItemModel.controller= NSStringFromClass([STMessageSetViewController class]);
    
    //赏个好评
    STSetingItemModel * scoreItemModel = [[STSetingItemModel alloc]init];
    scoreItemModel.leftIcon  = @"st_icon_nav_setting";
    scoreItemModel.leftTitle = @"赏个好评";
    scoreItemModel.itemType  = STSetingItemTypeNormal;
    scoreItemModel.jumpType  = STSetingItemJumpTypeAppUrl;
    
    //意见反馈
    STSetingItemModel * feedItemModel = [[STSetingItemModel alloc]init];
    feedItemModel.leftIcon  = @"st_icon_nav_setting";
    feedItemModel.leftTitle = @"意见反馈";
    feedItemModel.itemType  = STSetingItemTypeNormal;
    feedItemModel.jumpType  = STSetingItemJumpTypeNormal;
    feedItemModel.controller= NSStringFromClass([STFeedBackViewController class]);
    
    //关于我们
    STSetingItemModel * aboutItemModel = [[STSetingItemModel alloc]init];
    aboutItemModel.leftIcon  = @"st_icon_nav_setting";
    aboutItemModel.leftTitle = @"关于我们";
    aboutItemModel.itemType  = STSetingItemTypeNormal;
    aboutItemModel.jumpType  = STSetingItemJumpTypeNormal;
    aboutItemModel.controller= NSStringFromClass([STAboutUsViewController class]);
    
    //清除缓存
    STSetingItemModel * clearItemModel = [[STSetingItemModel alloc]init];
    clearItemModel.leftIcon  = @"st_icon_nav_setting";
    clearItemModel.leftTitle = @"清除缓存";
    clearItemModel.itemType  = STSetingItemTypeNormal;
    clearItemModel.jumpType  = STSetingItemJumpTypeClear;
    
    
    STSetingGroupModel * groupModel1 = [[STSetingGroupModel alloc]init];
    groupModel1.childItems = [@[themeItemModel,msgItemModel] mutableCopy];
    groupModel1.footerHeight = 15.0f;
    
    STSetingGroupModel * groupModel2 = [[STSetingGroupModel alloc]init];
    groupModel2.childItems = [@[scoreItemModel,feedItemModel,aboutItemModel] mutableCopy];
    groupModel2.footerHeight = 15.0f;
    
    STSetingGroupModel * groupModel3 = [[STSetingGroupModel alloc]init];
    groupModel3.childItems = [@[clearItemModel] mutableCopy];
    groupModel3.footerHeight = 15.0f;
    
    [self.dataArrs removeAllObjects];
    [self.dataArrs addObject:groupModel1];
    [self.dataArrs addObject:groupModel2];
    [self.dataArrs addObject:groupModel3];
    
    [self.tableView reloadData];
}

#pragma mark - set/get

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _tableView.dk_separatorColorPicker = DKColorPickerWithKey(SETTINGSEPARATOR);
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.rowHeight = 44;
        _tableView.dk_backgroundColorPicker = DKColorPickerWithKey(SETTINGSEPARATOR);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArrs{
    if (!_dataArrs) {
        _dataArrs = [[NSMutableArray alloc]init];
    }
    return _dataArrs;
}

#pragma mark - UITableViewDelegate/UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 44.0f;
    STSetingItemModel * itemModel = nil;
    STSetingGroupModel * groupModel = [self.dataArrs objectAtIndex:indexPath.section];
    if (groupModel && groupModel.childItems) {
        itemModel = [groupModel.childItems objectAtIndex:indexPath.row];
    }
    if (itemModel) {
        height = [STStingTableViewCell getCellHeightWithType:itemModel.itemType];
    }
    
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    STSetingGroupModel * groupModel = [self.dataArrs objectAtIndex:section];
    if (groupModel) {
        return groupModel.footerHeight;
    }
    return 0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = [[UIView alloc]init];
    footerView.dk_backgroundColorPicker = DKColorPickerWithKey(SETTINGSEPARATOR);
    return footerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STSetingItemModel * itemModel = nil;
    STSetingGroupModel * groupModel = [self.dataArrs objectAtIndex:indexPath.section];
    if (groupModel && groupModel.childItems) {
        itemModel = [groupModel.childItems objectAtIndex:indexPath.row];
    }
    [self jumpController:itemModel];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    STSetingGroupModel * groupModel = [self.dataArrs objectAtIndex:section];
    if (groupModel && groupModel.childItems) {
        return groupModel.childItems.count;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArrs ? [self.dataArrs count] : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    STSetingItemModel * itemModel = nil;
    STSetingGroupModel * groupModel = [self.dataArrs objectAtIndex:indexPath.section];
    if (groupModel && groupModel.childItems) {
        itemModel = [groupModel.childItems objectAtIndex:indexPath.row];
    }
    
    STStingTableViewCell * cell = [STStingTableViewCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.itemModel = itemModel;
    
    return cell;
}

#pragma mark - 页面跳转

- (void)jumpController:(STSetingItemModel *)itemModel{
    
    if (itemModel) {
        NSString * controller = itemModel.controller ? itemModel.controller : @"";
        switch (itemModel.itemType) {
            case STSetingItemJumpTypeNormal:
            {
                if (controller && NSClassFromString(controller)) {
                    Class class = NSClassFromString(controller);
                    UIViewController *vc = [[class alloc] init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
                break;
            case STSetingItemJumpTypePresent:
            {
                if (controller && NSClassFromString(controller)) {
                    Class class = NSClassFromString(controller);
                    UIViewController *vc = [[class alloc] init];
                    [self presentViewController:vc animated:YES completion:nil];
                }
            }
                break;
            case STSetingItemJumpTypeAppUrl:
            {
                
            }
                break;
            case STSetingItemJumpTypeClear:
            {
                
            }
                break;
            default:
                break;
        }
    }
}


@end
